package com.eshop.modules.quartz.utils;

import org.slf4j.LoggerFactory;
import org.quartz.JobDataMap;
import org.quartz.JobKey;
import org.quartz.CronTrigger;
import org.quartz.TriggerKey;
import org.quartz.Trigger;
import org.quartz.JobDetail;
import com.eshop.exception.BadRequestException;
import java.util.Date;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.quartz.ScheduleBuilder;
import org.quartz.CronScheduleBuilder;
import org.quartz.TriggerBuilder;
import org.quartz.JobBuilder;
import com.eshop.modules.quartz.domain.QuartzJob;
import javax.annotation.Resource;
import org.quartz.Scheduler;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

@Component
public class QuartzManage
{
    private static final Logger log;
    private static final String JOB_NAME = "TASK_";
    @Resource(name = "scheduler")
    private Scheduler scheduler;
    
    public void addJob(final QuartzJob quartzJob) {
        try {
            final JobDetail jobDetail = JobBuilder.newJob((Class)ExecutionJob.class).withIdentity("TASK_" + quartzJob.getId()).build();
            final Trigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("TASK_" + quartzJob.getId()).startNow().withSchedule((ScheduleBuilder)CronScheduleBuilder.cronSchedule(quartzJob.getCronExpression())).build();
            cronTrigger.getJobDataMap().put("JOB_KEY", quartzJob);
            ((CronTriggerImpl)cronTrigger).setStartTime(new Date());
            this.scheduler.scheduleJob(jobDetail, cronTrigger);
            if (quartzJob.getIsPause()) {
                this.pauseJob(quartzJob);
            }
        }
        catch (Exception e) {
            QuartzManage.log.error("创建定时任务失败", (Throwable)e);
            throw new BadRequestException("创建定时任务失败");
        }
    }
    
    public void updateJobCron(final QuartzJob quartzJob) {
        try {
            final TriggerKey triggerKey = TriggerKey.triggerKey("TASK_" + quartzJob.getId());
            CronTrigger trigger = (CronTrigger)this.scheduler.getTrigger(triggerKey);
            if (trigger == null) {
                this.addJob(quartzJob);
                trigger = (CronTrigger)this.scheduler.getTrigger(triggerKey);
            }
            final CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzJob.getCronExpression());
            trigger = (CronTrigger)trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule((ScheduleBuilder)scheduleBuilder).build();
            ((CronTriggerImpl)trigger).setStartTime(new Date());
            trigger.getJobDataMap().put("JOB_KEY", quartzJob);
            this.scheduler.rescheduleJob(triggerKey, (Trigger)trigger);
            if (quartzJob.getIsPause()) {
                this.pauseJob(quartzJob);
            }
        }
        catch (Exception e) {
            QuartzManage.log.error("更新定时任务失败", (Throwable)e);
            throw new BadRequestException("更新定时任务失败");
        }
    }
    
    public void deleteJob(final QuartzJob quartzJob) {
        try {
            final JobKey jobKey = JobKey.jobKey("TASK_" + quartzJob.getId());
            this.scheduler.pauseJob(jobKey);
            this.scheduler.deleteJob(jobKey);
        }
        catch (Exception e) {
            QuartzManage.log.error("删除定时任务失败", (Throwable)e);
            throw new BadRequestException("删除定时任务失败");
        }
    }
    
    public void resumeJob(final QuartzJob quartzJob) {
        try {
            final TriggerKey triggerKey = TriggerKey.triggerKey("TASK_" + quartzJob.getId());
            final CronTrigger trigger = (CronTrigger)this.scheduler.getTrigger(triggerKey);
            if (trigger == null) {
                this.addJob(quartzJob);
            }
            final JobKey jobKey = JobKey.jobKey("TASK_" + quartzJob.getId());
            this.scheduler.resumeJob(jobKey);
        }
        catch (Exception e) {
            QuartzManage.log.error("恢复定时任务失败", (Throwable)e);
            throw new BadRequestException("恢复定时任务失败");
        }
    }
    
    public void runJobNow(final QuartzJob quartzJob) {
        try {
            final TriggerKey triggerKey = TriggerKey.triggerKey("TASK_" + quartzJob.getId());
            final CronTrigger trigger = (CronTrigger)this.scheduler.getTrigger(triggerKey);
            if (trigger == null) {
                this.addJob(quartzJob);
            }
            final JobDataMap dataMap = new JobDataMap();
            dataMap.put("JOB_KEY", quartzJob);
            final JobKey jobKey = JobKey.jobKey("TASK_" + quartzJob.getId());
            this.scheduler.triggerJob(jobKey, dataMap);
        }
        catch (Exception e) {
            QuartzManage.log.error("定时任务执行失败", (Throwable)e);
            throw new BadRequestException("定时任务执行失败");
        }
    }
    
    public void pauseJob(final QuartzJob quartzJob) {
        try {
            final JobKey jobKey = JobKey.jobKey("TASK_" + quartzJob.getId());
            this.scheduler.pauseJob(jobKey);
        }
        catch (Exception e) {
            QuartzManage.log.error("定时任务暂停失败", (Throwable)e);
            throw new BadRequestException("定时任务暂停失败");
        }
    }
    
    static {
        log = LoggerFactory.getLogger((Class)QuartzManage.class);
    }
}
